/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.gruposicom.dao.implement;

import ar.com.gruposicom.dao.CommandDao;
import ar.com.gruposicom.domain.Command;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marcelo
 */
public class CommandDaoImp extends DataManager implements CommandDao
{
    
    private Statement stmt;
    private Connection conn;

    @Override
    public int save(Command c, int idOperacion) 
    {
        int nuevoId = 0;
        try {
            ResultSet resu = null;
            try 
            { 
                conn = super.getConection();
                stmt = conn.createStatement();
                stmt.executeUpdate("INSERT INTO comandos (operacion_id, comando) VALUES ("
                +idOperacion+",'"+c.getCommand()+"')");  
                resu = stmt.executeQuery("Select Max(comando_id) From comandos");
            }
            catch (SQLException ex)
            {
              while (ex !=null)
                {
                ex.printStackTrace();
                ex = ex.getNextException();
                }
            }
            resu.next();
            nuevoId = resu.getInt(1);
            this.cerrar();
        } catch (SQLException ex) {
            Logger.getLogger(CommandDaoImp.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nuevoId;
    }

    @Override
    public Collection<Command> getAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Command get(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete(Command c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void modify(Command c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    private void cerrar()
    {
      try
      {
        stmt.close();
        conn.close();
      }
      catch(SQLException e)
      {
        while (e != null) 
        {
          e.printStackTrace();
          e = e.getNextException();
        }        
      }      
    }

    @Override
    public Collection<Command> getAllFromOperation(int id) 
    {
        ResultSet resu = null;
        try 
        { 
            conn = super.getConection();      
            stmt = conn.createStatement();  
            resu = stmt.executeQuery("SELECT comando_id, comando "
                    + "FROM comandos "
                    + "WHERE operacion_id = "+id); 
        }
        catch (SQLException ex)
        {
            while (ex !=null)
            {
                ex.printStackTrace();
                ex = ex.getNextException();
            }
        } 
        Collection<Command> co = new ArrayList<>();   
        try
        {
            while (resu.next())
            {
                Command c = new Command();
                c.setId(resu.getInt(1));
                c.setCommand(resu.getString(2).trim());
                co.add(c);
            }
        }
        catch(SQLException ex)
        {
            while (ex !=null)
            {
                ex.printStackTrace();
                ex = ex.getNextException();
            }
        }
        this.cerrar();            
        return co; 
    }
    
}
